package org.yray.license;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.nio.file.Paths;

@Slf4j
@Component
public class LicenseCheckRunner implements ApplicationRunner {

    @Value("${license.subject}")
    private String subject;

    @Value("${license.publicAlias}")
    private String publicAlias;

    @Value("${license.storePass}")
    private String storePass;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("++++++++ 开始安装证书 ++++++++");
        LicenseVerifyParam param = new LicenseVerifyParam();
        param.setSubject(subject);
        param.setPublicAlias(publicAlias);
        param.setStorePass(storePass);

        String resourcePath = Paths.get("").toString();//getClass().getClassLoader().getResource("").getPath();
        param.setLicensePath(resourcePath + "license.lic");
        param.setPublicKeysStorePath(resourcePath + "publicCerts.keystore");

        LicenseVerify licenseVerify = new LicenseVerify();
        licenseVerify.install(param);
        log.info("++++++++ 证书安装结束 ++++++++");
    }
}